#include <jni.h>
#include <string>

#define LOG_TAG "System.out"


#include<android/log.h>
#include <stdio.h>
#include "stdlib.h"
#include "string.h"

#pragma comment (lib, "BaseInfo.lib")
#pragma comment (lib, "jvm.lib")
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
extern "C"
JNIEXPORT jfloat JNICALL
Java_com_dingqb_ndkproject_nativelib_ww_add(JNIEnv *env, jclass type, jfloat a, jfloat b) {

    return a + b;
}

extern "C"
JNIEXPORT jfloat JNICALL
Java_com_dingqb_ndkproject_nativelib_ww_mulit(JNIEnv *env, jclass type, jfloat a, jfloat b) {

    return a * b;
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_dingqb_ndkproject_nativelib_ww_returnSelf(JNIEnv *env, jclass type, jint a) {

    int a_ = a;
    int b_ = a;
    int c_ = a_ + b_ - a;
    return c_;

}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_replacePersonName(JNIEnv *env, jclass type, jstring oldName) {
    jstring newStr = oldName;
    const char *chart = "Java_com_dingqb_ndkproject_nativelib_Jni_replacePersonName()---";
    jstring s2 = env->NewStringUTF(chart);
    LOGE("dingqb%s", chart);
    return newStr;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_booleanValue(JNIEnv *env, jclass type, jboolean b) {
    if (b) {
        LOGE("dingqb true ");
        LOGE("dingqbBoolean==%lu ", b);
    } else {
        LOGE("dingqb false");
        LOGE("dingqbBoolean==%lu ", b);
    }
}
extern "C"
JNIEXPORT jobjectArray JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_returnStringArray(JNIEnv *env, jclass type) {

    jstring str;
    jobjectArray args = 0;
    jsize len = 6;
    char *sa[] = {"Hello ", "world!", "JNI ", "is ", "funny", "I want to fuck it!"};
    jclass jclass_ = (*env).FindClass("java/lang/String");
    args = (env)->NewObjectArray(len, jclass_, 0);
    for (int i = 0; i < len; i++) {
        char *c = sa[i];
        str = (env)->NewStringUTF(c);
        (env)->SetObjectArrayElement(args, i, str);
    }
    return args;
}extern "C"
JNIEXPORT void JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_stringArray(JNIEnv *env, jobject instance, jobjectArray stringArray) {
    int count = env->GetArrayLength(stringArray);
    for (int i = 0; i < count; i++) {
        jobject item = env->GetObjectArrayElement(stringArray, i);
        const char *str = env->GetStringUTFChars((jstring) item, 0);
        LOGE("dingqb stringArray---index:%d string:%s", i, str);
        env->ReleaseStringUTFChars((jstring) item, str);

    }


}extern "C"
JNIEXPORT void JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_floatArray(JNIEnv *env, jobject instance, jfloatArray floatArray) {
    jfloat *array = env->GetFloatArrayElements(floatArray, 0);
    int count = env->GetArrayLength(floatArray);
    for (int i = 0; i < count; i++) {
        LOGE("dingqb floatArray---index:%d float:%f", i, array[i]);
    }
    env->ReleaseFloatArrayElements(floatArray, array, 0);
}extern "C"
JNIEXPORT jstring JNICALL
Java_com_dingqb_ndkproject_nativelib_ww_returnSelfString(JNIEnv *env, jclass type, jstring self_) {
    const char *self = env->GetStringUTFChars(self_, 0);
    LOGE("dingqb reruenString==%s", self);
    return env->NewStringUTF(self);
}extern "C"
JNIEXPORT jobjectArray JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_returnSringArray(JNIEnv *env, jclass type, jobjectArray array) {
    // 输出字符数组
    int len = env->GetArrayLength(array);
    for (int i = 0; i < len; ++i) {
        jobject jobject_ = env->GetObjectArrayElement(array, i);
        jstring jstring_ = static_cast<jstring>(jobject_);
        const char *szStr = env->GetStringUTFChars(jstring_, 0);
        LOGE("dingqb %s", szStr);
        env->ReleaseStringUTFChars(jstring_, szStr);


    }
    // 返回字符串数组
    int size = 6;
    jclass jclass_ = env->FindClass("java/lang/String");
    jobjectArray args = env->NewObjectArray(size, jclass_, 0);
    char *sa[] = {"Hello ", "world!", "JNI ", "is ", "funny.", "I want to fuck it!"};
    for (int i = 0; i < size; ++i) {
        jstring str = env->NewStringUTF(sa[i]);
        env->SetObjectArrayElement(args, i, str);
    }
    return args;

}extern "C"
JNIEXPORT jint JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_getJavaIntField(JNIEnv *env, jobject instance) {
    //1.获得实例对应的class类
    jclass jcls = (*env).GetObjectClass(instance);
    //2.通过class类找到对应的field id
    jfieldID fid = env->GetFieldID(jcls, "num", "I");
    //3.通过实例object获得对应的field
    jint jnum = env->GetIntField(instance, fid);
    //add
    jnum += 100;
    return jnum;

}extern "C"
JNIEXPORT jstring JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_getJavaField(JNIEnv *env, jobject instance) {

    // TODO : 获取java属性，非静态的属性。当是静态的属性时Error
    jclass jclass_ = env->GetObjectClass(instance);
    jfieldID jfieldID_ = env->GetFieldID(jclass_, "TAG", "Ljava/lang/String;");
    jobject jobject_ = env->GetObjectField(instance, jfieldID_);
    jstring jstring_ = static_cast<jstring>(jobject_);
    const char *charat = env->GetStringUTFChars(jstring_, 0);
    LOGE("dingqb%s", charat);
    const char *newChart = "set class field newvalues from jni ";
    LOGE("dingqb%s", newChart);
    jstring newStr = env->NewStringUTF(newChart);
    // 设置新的数值
    env->SetObjectField(instance, jfieldID_, newStr);
    jobject_ = env->GetObjectField(instance, jfieldID_);
    jstring_ = static_cast<jstring>(jobject_);
    charat = env->GetStringUTFChars(jstring_, 0);
    LOGE("dingqb new values %s", charat);
    return jstring_;

}

void logString(const char *console) {
    LOGE("dingqingbo---%s", console);
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_getstaticJavaField(JNIEnv *env, jobject instance) {
    jclass jclass_ = env->GetObjectClass(instance);
    jfieldID jfieldID_ = env->GetStaticFieldID(jclass_, "staticValue", "Ljava/lang/String;");
    jobject jobject_ = env->GetStaticObjectField(jclass_, jfieldID_);
    jstring jstring_ = static_cast<jstring>(jobject_);
    const char *chart = env->GetStringUTFChars(jstring_, 0);
    LOGE("dingqb %s", chart);
    logString(chart);
    return env->NewStringUTF(chart);
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_staticMethod(JNIEnv *env, jclass jclass_) {
    jfieldID jfieldID_ = env->GetStaticFieldID(jclass_, "staticValue", "Ljava/lang/String;");
    jobject jobject_ = env->GetStaticObjectField(jclass_, jfieldID_);
    jstring jstring_ = static_cast<jstring>(jobject_);
    const char *chart = env->GetStringUTFChars(jstring_, 0);
    return env->NewStringUTF(chart);
}extern "C"
JNIEXPORT jstring JNICALL
Java_com_dingqb_ndkproject_nativelib_Jni_staticMethodNotStaticField(JNIEnv *env, jclass jclass_) {
//    // 获取jobject
//    jobject jobject_ = env->AllocObject(jclass_);
//
//    //得到jfieldID
//    jfieldID jfieldID_ = env->GetFieldID(jclass_, "name", "Ljava/lang/String;");
//    //得到 name属性值
//    jobject jobject_name = env->GetObjectField(jobject_, jfieldID_);
//    jstring jstring_name = static_cast<jstring>(jobject_name);
//    const char *chart = env->GetStringUTFChars(jstring_name, 0);
//    logString(chart);
//    return jstring_name;

    const  char* chara="error";
    return env->NewStringUTF(chara);


}